package com.phone.modules.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phone.modules.pms.model.PmsProduct;
import com.phone.modules.pms.service.PmsProductService;
import com.phone.modules.rms.dto.RmsChargeResultDTO;
import com.phone.modules.rms.dto.RmsQueryDTO;
import com.phone.modules.rms.model.RmsCharge;
import com.phone.modules.rms.mapper.RmsChargeMapper;
import com.phone.modules.rms.service.RmsChargeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 充电排行表 服务实现类
 * </p>
 *
 * @author WangHao
 * @since 2022-04-18
 */
@Service
public class RmsChargeServiceImpl extends ServiceImpl<RmsChargeMapper, RmsCharge> implements RmsChargeService {

    @Autowired
    PmsProductService productService;

    @Autowired
    RmsChargeMapper chargeMapper;

    /**
     * 前台展示-检索充电信息,分页
     * @param queryDTO
     * @return
     */
    @Override
    public Page searchList(RmsQueryDTO queryDTO) {
        List<RmsChargeResultDTO> resultDTOList = chargeMapper.searchList(queryDTO);
        if (Objects.equals(queryDTO.getOrder(), "tab1")) {
            // 如果是tab1，需要对10分钟充入点亮百分比进行排序，逆序
            resultDTOList.sort(Comparator.comparing(RmsChargeResultDTO::getTenPower).reversed());
        } else if (Objects.equals(queryDTO.getOrder(), "tab2")) {
            // 如果是tab2，需要对手机前台显示100%充电时间进行排序，正序
            resultDTOList.sort(Comparator.comparing(RmsChargeResultDTO::getUiTime));
            for (RmsChargeResultDTO chargeResultDTO : resultDTOList) {
                chargeResultDTO.setPer(Double.valueOf(resultDTOList.get(resultDTOList.size()-1).getUiTime()));
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab3")) {
            // 如果是tab3，需要对手机玩完全充满时间进行排序，正序
            resultDTOList.sort(Comparator.comparing(RmsChargeResultDTO::getTotalTime));
            for (RmsChargeResultDTO chargeResultDTO : resultDTOList) {
                chargeResultDTO.setPer(Double.valueOf(resultDTOList.get(resultDTOList.size()-1).getTotalTime()));
            }
        }
        Page page = new Page(queryDTO.getPageNum(), queryDTO.getPageSize());
        page.setRecords(resultDTOList);
        page.setTotal(chargeMapper.getCount(queryDTO));
        return page;
    }

    /**
     * 分页获取充电信息列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page listCharge(String keyword, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<RmsCharge> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.lambda().like(RmsCharge::getProductName, keyword);
        }
        // 排序
        queryWrapper.lambda().orderByAsc(RmsCharge::getTotalTime);
        return this.page(page, queryWrapper);
    }

    /**
     * 新增充电信息
     * @param charge
     * @return
     */
    @Override
    public boolean saveCharge(RmsCharge charge) {
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PmsProduct::getProductName, charge.getProductName());
        PmsProduct product = productService.getOne(queryWrapper);
        if (product != null) {
            charge.setProductId(product.getId());
        }
        return this.save(charge);
    }

    /**
     * 更新充电信息
     * @param id
     * @param charge
     * @return
     */
    @Override
    public boolean updateCharge(Long id, RmsCharge charge) {
        charge.setId(id);
        return this.updateById(charge);
    }

    /**
     * 批量删除充电信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteCharge(List<Long> ids) {
        return removeByIds(ids);
    }

}
